using CodePen.Server.Language;
namespace CodePen.Web.Controllers;

/// <summary>
/// 
/// </summary>
[Authorize]
public partial class ContentController : Controller
{
    /// <summary>
    /// 
    /// </summary>
    private readonly ContentService _contentService;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="contentService"></param> 
    public ContentController(ContentService contentService)
    {
        _contentService = contentService; 
    }

    public IActionResult Index()
    {
        return View();
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("content/pages")]
    public async Task<RestfulResult<PageResult<PickContent>>> GetPagesAsync()
    {
        using var sr = new StreamReader(Request.Body);
        string dataStr = await sr.ReadToEndAsync();
        if (Request.Body.CanSeek)
            Request.Body.Position = 0;
        if (string.IsNullOrWhiteSpace(dataStr))
            throw new Exception(i18n.upload_data_required);
        ContentPageParam? data = JsonConvert.DeserializeObject<ContentPageParam>(dataStr);
        data ??= new ContentPageParam { PageNo = 1, PageSize = 10 };

        var query = await _contentService.GetPagesAsync(data);
        return query;
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [AllowAnonymous,HttpPost("content/allpages")]
    public async Task<RestfulResult<PageResult<PickContent>>> GetAllPagesAsync()
    {
        using var sr = new StreamReader(Request.Body);
        string dataStr = await sr.ReadToEndAsync();
        if (Request.Body.CanSeek)
            Request.Body.Position = 0;
        if (string.IsNullOrWhiteSpace(dataStr))
            throw new Exception(i18n.upload_data_required);
        ContentPageParam? data = JsonConvert.DeserializeObject<ContentPageParam>(dataStr);
        data ??= new ContentPageParam { PageNo = 1, PageSize = 10 };

        var query = await _contentService.GetAllPagesAsync(data);
        return query;
    }
    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <returns></returns>
    [HttpPost("content/list")]
    public async Task<RestfulResult<PageResult<PickContent>>> GetlistAsync()
    {
        var restful = await _contentService.GetlistAsync();
        return restful;
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    [HttpPost("content/add")]
    public async Task<RestfulResult> AddAsync([FromBody] PickContent data)
    {
        var restful = await _contentService.AddAsync(data);
        return restful;
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("content/update")]
    public async Task<RestfulResult> UpdateAsync([FromBody] PickContent data)
    {
        var restful = await _contentService.UpdateAsync(data);
        return restful;
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("content/delete")]
    public async Task<RestfulResult> DeleteAsync([FromBody] EntityBaseId data)
    {
        var restful = await _contentService.DeleteAsync(data);
        return restful;
    }

    // 模板下载功能已迁移到前端处理

    /// <summary>
    /// 导入数据
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    [HttpPost("content/import")]
    public async Task<RestfulResult<object>> ImportAsync(IFormFile file)
    {
        if (file == null || file.Length == 0)
        {
            throw new Exception(i18n.upload_data_required);
        }

        // 验证文件类型
        var allowedExtensions = new[] { ".xlsx" };
        var fileExtension = Path.GetExtension(file.FileName).ToLowerInvariant();
        if (!allowedExtensions.Contains(fileExtension))
        {
            throw new Exception("请上传XLSX文件(.xlsx格式)");
        }

        using var memoryStream = new MemoryStream();
        await file.CopyToAsync(memoryStream);
        var fileContent = memoryStream.ToArray();
        
        var restful = await _contentService.ImportAsync(fileContent);
        return restful;
    }
}

